<template>
    
    <!-- 组件的模板 -->
    <div class="page detail" style="padding-bottom: 46px;">
        <div class="top-section com-border-bottom">
            <h2>{{adDetails.title}}</h2>
            <p class="p1">
                <span>{{adDetails.create_at | formatDateTime}}</span>
                <!-- <a href="#">{{adDetails.creator_name}}</a> -->
                <span class="view"><i class="bg_sprite icon-view"></i>{{adDetails.see}}</span>
            </p>   
            <p class="p2" v-if="adDetails.is_receipt == 1 && adDetails.report_count != undefined">
                <a href="javascript:;" @click="gotoConfirm">已确认{{adDetails.report_count}}人</a>
                <a href="javascript:;" @click="gotoConfirmnot">未确认{{adDetails.noreport_count}}人</a>
            </p>
        </div>
        <div class="content"> 
            <p style="word-wrap:break-word" v-for="text in adDetails.desc" v-html="text"></p>
            <img class="pic" :src="'http://image.doumi.com/'+item" alt="" v-for="item in adDetails.img_url" @click.prevent="showBigImg($index)">
        </div>
        <div v-if="adDetails.is_receipt == 1 && recBtn_show">  <!-- report_status == 1 可点击  report_status == 0 不可点击 -->
            <a href="javascript:;" v-if="adDetails.report_status == 1" @click="confirm" class="weui-btn weui-btn_primary fixed" :style="isIphoneX?'height:60px;':''">确认收到</a>
            <a href="javascript:;" v-else class="weui-btn weui-btn_primary weui-btn_disabled fixed" :style="isIphoneX?'height:60px;':''">已确认</a>
        </div>
    </div>  
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import * as util from 'js/util.js'
    import widget from 'gapi/widget'
    import commonPage from 'clientApi/commonPage.js'
    import clientUi from 'gapi/clientUi'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                isshow:false,
                viewconfirm:false,
                read:'',
                base:{
                    teamid:'',
                    projectid:'',
                    state:'',
                    noticeid:'',
                    root:'' 
                },
                adDetails:{},
                recBtn_show:false,
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        computed: {
            params (){
                let str = 'noticeId='+this.noticeId;
                return encodeURIComponent(str)
            },
            isIphoneX: function(){
                return util.isIphoneX()
            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                
                let teamid = util.getUrddataString('teamId');
                let projectid = util.getUrddataString('projectId');
                let noticeid = util.getUrddataString('noticeId');
                let read = util.getUrddataString('read')
                let root = util.getUrddataString('root')
                this.base.teamid = teamid;
                this.base.projectid = projectid;
                this.base.noticeid = noticeid;
                this.base.root = root;
                this.read = read
                this.initAjax();
                this.getPermissions();
            },
            initAjax (){
                util.ajax({
                    url:'/notice/user/detail',
                    type:'GET',
                    data:{
                        team_id: this.base.teamid,
                        project_id: this.base.projectid,
                        notice_id: this.base.noticeid
                    },
                    timeout:10000,
                    success:(obj) => {
                        if(obj&&obj.errno == 0){
                            let descary = obj.data.desc.split("#br#"),
                                reg = /(http:\/\/|https:\/\/|HTTP:\/\/|HTTPS:\/\/|www|WWW)((\w|=|\?|\.|\/|&|-)+)/g;
                            console.log('公告详情'+JSON.stringify(obj.data))
                            this.adDetails = obj.data;
                            for(let i = 0,lg = descary.length;i < lg;i++){
                                descary[i] = descary[i].replace(reg, function(data){
                                    let reghttp = /^http:\/\/|https:\/\/|HTTP:\/\/|HTTPS:\/\//,
                                        newurl;
                                    if(!data.match(reghttp)){
                                        newurl = 'http://' + data;
                                    }else{
                                        newurl = data;
                                    }
                                    return '<a style="color:#4A90E2;text-decoration:underline" href="dmsaas://dmbrowser/' + newurl +'">'+ data +'</a>'
                                })
                            }
                            this.adDetails.desc = descary;
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            /*跳转---已确认*/
            gotoConfirm (){
                let encodeStr = encodeURIComponent(`teamId=${this.base.teamid}&projectId=${this.base.projectid}&noticeId=${this.base.noticeid}&title=${this.adDetails.title}&true=${true}`)
                location.href = `dmsaas://AdConfirmList?urdData=${encodeStr}`
            },
            gotoConfirmnot (){
                let encodeStr = encodeURIComponent(`teamId=${this.base.teamid}&projectId=${this.base.projectid}&noticeId=${this.base.noticeid}&title=${this.adDetails.title}&desc=${this.adDetails.see}`)
                location.href = `dmsaas://AdConfirmList?urdData=${encodeStr}`
            },
            /*确认收到*/
            confirm (){
                let t = this;
                util.ajax({
                    url:'/notice/user/report',
                    type:'GET',
                    data:{
                        team_id: this.base.teamid,
                        project_id: this.base.projectid,
                        notice_id: this.base.noticeid,
                        status:1
                    },
                    timeout:10000,
                    success:(obj) => {
                        if(obj&&obj.errno == 0){
                            this.adDetails.report_status = 0
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            //获取权限
            getPermissions(){
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/permission/application',
                    type:'GET',
                    data:{
                        team_id:this.base.teamid,
                        project_id:this.base.projectid,
                        application_id:0
                    },
                    timeout:10000,
                    success:(obj) => {
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj&&obj.errno == 0){
                            obj.data.forEach ( (i) => {
                                if(i.id_name && i.id_name == 'notice_report_button'){ 
                                    this.recBtn_show = true;
                                }

                            })
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            //查看大图
            showBigImg (index){
                let imgs = this.adDetails.img_url
                clientUi.bigPicturePreview('预览', imgs, index)
            }
        },
        filters: {
            formatDateTime(date){
                return util.formatDate(date);
            }
        },
        created(){

            this.init();

             /*设置Native的头部*/
            commonPage.updateTitleBar({
                backAction: 'checkChange',    // 点击返回按钮的动作（即回调函数名称）
                leftDisplay: false,            // 是否显示左按钮
                leftText: '',                // 左按钮文案
                leftAction: 'checkChange'    // 点击左按钮的动作（即回调函数名称）
                // rightDisplay: false,            // 是否显示右按钮
                // rightText: '',            // 右按钮文案
                // rightAction: ''    // 点击右按钮的文案（即回调函数名称）
            })
            window.checkChange = () => {
                if(this.read){
                    commonPage.goBack(1,{'key':2})
                }else{
                    commonPage.goBack(1,{'key':1})
                }
            }
            // 返回按钮native设置
        }
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/notice.css"></style>



